package cn.felord.authorization.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试接口
 *
 * @author dax.
 * @version v1.0
 * @since 2018/1/8 14:55
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @PreAuthorize("hasRole('ROLE_TRUSTED_CLIENT')")
    public User get() {

        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();

        String name=authentication.getName();
        System.out.println(name);
        Object user=   authentication.getPrincipal();
        System.out.println(user);
        return new User("AAAAA", "dfdafasdfsdaf", AuthorityUtils.createAuthorityList("ROLE_ADMIN"));
    }
}
